<template>
    <div class="achievement-card" :class="{
        unlocked: achievement.is_unlocked,
        featured: achievement.is_featured,
        'in-progress': !achievement.is_unlocked && achievement.progress
    }" @click="handleClick">
        <!-- 特色标识 -->
        <div v-if="achievement.is_featured" class="featured-badge">
            <van-icon name="fire" />
            <span>Featured</span>
        </div>

        <!-- 成就图标 -->
        <div class="achievement-icon">
            <van-image :src="achievement.icon_url || '/default-achievement.png'" width="64" height="64" fit="cover"
                :class="{ grayscale: !achievement.is_unlocked }" />

            <!-- 锁定遮罩 -->
            <div v-if="!achievement.is_unlocked" class="lock-overlay">
                <van-icon name="lock" />
            </div>

            <!-- 解锁光效 -->
            <div v-if="achievement.is_unlocked" class="unlock-glow"></div>
        </div>

        <!-- 成就信息 -->
        <div class="achievement-info">
            <h4 class="achievement-title">{{ achievement.title }}</h4>
            <p class="achievement-description">{{ achievement.description }}</p>

            <!-- 奖励点数 -->
            <div class="achievement-points">
                <van-icon name="diamond-o" />
                <span>{{ achievement.points }} pts</span>
            </div>
        </div>

        <!-- 进度信息 -->
        <div class="achievement-status">
            <!-- 已解锁状态 -->
            <div v-if="achievement.is_unlocked" class="unlocked-status">
                <div class="status-badge">
                    <van-icon name="success" />
                    <span>Unlocked</span>
                </div>
                <div class="unlock-date">
                    {{ formatDate(achievement.unlocked_at) }}
                </div>
            </div>

            <!-- 进行中状态 -->
            <div v-else-if="achievement.progress" class="progress-status">
                <div class="progress-info">
                    <span class="progress-text">
                        {{ achievement.progress.current }}/{{ achievement.progress.target }}
                    </span>
                    <span class="progress-percentage">
                        {{ Math.round((achievement.progress.current / achievement.progress.target) * 100) }}%
                    </span>
                </div>
                <van-progress :percentage="(achievement.progress.current / achievement.progress.target) * 100"
                    stroke-width="6" color="#1989fa" track-color="#f5f5f5" />
                <div class="progress-tip">
                    {{ getProgressTip }}
                </div>
            </div>

            <!-- 锁定状态 -->
            <div v-else class="locked-status">
                <div class="status-badge locked">
                    <van-icon name="lock" />
                    <span>Locked</span>
                </div>
                <div class="unlock-hint">
                    Tap to see requirements
                </div>
            </div>
        </div>

        <!-- 操作按钮 -->
        <div class="achievement-actions">
            <van-button v-if="achievement.is_unlocked" size="mini" type="primary" plain @click.stop="handleShare">
                <van-icon name="share-o" />
                Share
            </van-button>
            <van-button v-else-if="achievement.progress" size="mini" type="primary" @click.stop="handleStartTraining">
                Continue
            </van-button>
            <van-button v-else size="mini" @click.stop="handleViewRequirements">
                View Requirements
            </van-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { showToast } from 'vant'
import type { Achievement } from '@/api/achievement'

interface Props {
    achievement: Achievement
}

interface Emits {
    (e: 'click', achievement: Achievement): void
    (e: 'share', achievement: Achievement): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 计算属性
const getProgressTip = computed(() => {
    if (!props.achievement.progress) return ''

    const { current, target } = props.achievement.progress
    const remaining = target - current

    if (remaining <= 1) {
        return 'Almost there! One more to go.'
    } else if (remaining <= 3) {
        return `${remaining} more needed. Keep going!`
    } else {
        return `${remaining} more to unlock this achievement.`
    }
})

// 方法
const formatDate = (dateString?: string) => {
    if (!dateString) return ''

    const date = new Date(dateString)
    const now = new Date()
    const diffInDays = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24))

    if (diffInDays === 0) {
        return 'Unlocked today'
    } else if (diffInDays === 1) {
        return 'Unlocked yesterday'
    } else if (diffInDays < 7) {
        return `Unlocked ${diffInDays} days ago`
    } else {
        return `Unlocked on ${date.toLocaleDateString()}`
    }
}

const handleClick = () => {
    emit('click', props.achievement)
}

const handleShare = () => {
    emit('share', props.achievement)
}

const handleStartTraining = () => {
    showToast('Redirecting to training...')
    // 这里可以跳转到相关的训练页面
}

const handleViewRequirements = () => {
    emit('click', props.achievement)
}
</script>

<style lang="scss" scoped>
.achievement-card {
    position: relative;
    background: white;
    border-radius: 16px;
    padding: 20px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 2px solid transparent;
    overflow: hidden;

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    }

    &.unlocked {
        border-color: #07c160;
        background: linear-gradient(135deg, #f0f9ff 0%, #e0f7fa 100%);

        &:hover {
            box-shadow: 0 8px 24px rgba(7, 193, 96, 0.2);
        }
    }

    &.featured {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;

        .achievement-title,
        .achievement-description,
        .achievement-points span {
            color: white;
        }

        .progress-status {

            .progress-text,
            .progress-percentage,
            .progress-tip {
                color: rgba(255, 255, 255, 0.9);
            }
        }

        .locked-status {
            .unlock-hint {
                color: rgba(255, 255, 255, 0.8);
            }
        }
    }

    &.in-progress {
        border-color: #1989fa;
        background: linear-gradient(135deg, #f0f8ff 0%, #e8f4fd 100%);
    }

    .featured-badge {
        position: absolute;
        top: 12px;
        right: 12px;
        display: flex;
        align-items: center;
        gap: 4px;
        background: linear-gradient(135deg, #ee0a24, #ff6b35);
        color: white;
        padding: 4px 8px;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 600;
        z-index: 2;

        .van-icon {
            font-size: 12px;
        }
    }

    .achievement-icon {
        position: relative;
        display: flex;
        justify-content: center;
        margin-bottom: 16px;

        .van-image {
            border-radius: 12px;
            transition: filter 0.3s ease;

            &.grayscale {
                filter: grayscale(100%) brightness(0.7);
            }
        }

        .lock-overlay {
            position: absolute;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 64px;
            height: 64px;
            background: rgba(0, 0, 0, 0.6);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;

            .van-icon {
                color: white;
                font-size: 20px;
            }
        }

        .unlock-glow {
            position: absolute;
            top: -4px;
            left: 50%;
            transform: translateX(-50%);
            width: 72px;
            height: 72px;
            background: linear-gradient(45deg, #ffd21e, #ff6b35, #1989fa, #07c160);
            border-radius: 16px;
            z-index: -1;
            animation: glow 2s ease-in-out infinite alternate;
        }
    }

    .achievement-info {
        text-align: center;
        margin-bottom: 16px;

        .achievement-title {
            font-size: 18px;
            font-weight: 600;
            color: #323233;
            margin: 0 0 8px 0;
            line-height: 1.3;
        }

        .achievement-description {
            font-size: 14px;
            color: #646566;
            line-height: 1.4;
            margin: 0 0 12px 0;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .achievement-points {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            font-size: 14px;
            font-weight: 500;

            .van-icon {
                color: #1989fa;
                font-size: 16px;
            }

            span {
                color: #646566;
            }
        }
    }

    .achievement-status {
        margin-bottom: 16px;

        .unlocked-status {
            text-align: center;

            .status-badge {
                display: inline-flex;
                align-items: center;
                gap: 4px;
                background: #07c160;
                color: white;
                padding: 4px 8px;
                border-radius: 12px;
                font-size: 12px;
                font-weight: 500;
                margin-bottom: 6px;

                .van-icon {
                    font-size: 14px;
                }
            }

            .unlock-date {
                font-size: 12px;
                color: #969799;
            }
        }

        .progress-status {
            .progress-info {
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-bottom: 8px;
                font-size: 13px;

                .progress-text {
                    color: #323233;
                    font-weight: 500;
                }

                .progress-percentage {
                    color: #1989fa;
                    font-weight: 600;
                }
            }

            .progress-tip {
                font-size: 12px;
                color: #646566;
                text-align: center;
                margin-top: 8px;
                line-height: 1.3;
            }
        }

        .locked-status {
            text-align: center;

            .status-badge {
                display: inline-flex;
                align-items: center;
                gap: 4px;
                background: #969799;
                color: white;
                padding: 4px 8px;
                border-radius: 12px;
                font-size: 12px;
                font-weight: 500;
                margin-bottom: 6px;

                .van-icon {
                    font-size: 14px;
                }
            }

            .unlock-hint {
                font-size: 12px;
                color: #969799;
            }
        }
    }

    .achievement-actions {
        display: flex;
        justify-content: center;

        .van-button {
            min-width: 80px;

            .van-icon {
                margin-right: 4px;
            }
        }
    }
}

@keyframes glow {
    0% {
        opacity: 0.5;
        transform: translateX(-50%) scale(1);
    }

    100% {
        opacity: 0.8;
        transform: translateX(-50%) scale(1.05);
    }
}
</style>